/*
 * Copyright (C) 2015 备胎金服
 * 未经授权允许不得进行拷贝和修改
 *   http://www.btjf.com/
 */
package com.zhangman.androidgame;

import android.graphics.Rect;
import java.util.ArrayList;

/**
 * Created by zhangman on 2016/11/1 16:07.
 * Email: zhangman523@126.com
 */
public class Robot {
  final int JUMPSPEED = -15;
  final int MOVESPEED = 5;

  private int centerX = 100;
  private int centerY = 377;
  private boolean jumped = false;
  private boolean movingLeft = false;
  private boolean movingRight = false;
  private boolean ducked = false;
  private boolean readyToFire = true;

  private int speedX = 0;
  private int speedY = 0;
  public static Rect rect = new Rect(0, 0, 0, 0);
  public static Rect rect2 = new Rect(0, 0, 0, 0);
  public static Rect rect3 = new Rect(0, 0, 0, 0);
  public static Rect rect4 = new Rect(0, 0, 0, 0);
  public static Rect yellowRed = new Rect(0, 0, 0, 0);

  public static Rect footleft = new Rect(0, 0, 0, 0);
  public static Rect footright = new Rect(0, 0, 0, 0);

  private Background bg1 = GameScreen.getBg1();
  private Background bg2 = GameScreen.getBg2();
  private ArrayList<Projectile> mProjectiles = new ArrayList<>();

  public void update() {
    if (speedX < 0) {
      centerX += speedX;
    }
    if (speedX == 0 || speedX < 0) {
      bg1.setSpeedX(0);
      bg2.setSpeedX(0);
    }
    if (centerX <= 200 && speedX > 0) {
      centerX += speedX;
    }
    if (speedX > 0 && centerX > 200) {
      bg1.setSpeedX(-MOVESPEED / 5);
      bg2.setSpeedX(-MOVESPEED / 5);
    }

    centerY += speedY;

    speedY += 1;
    if (speedY > 3) {
      jumped = true;
    }
    if (centerX + speedX <= 60) {
      centerX = 61;
    }

    rect.set(centerX - 34, centerY - 64, centerX + 34, centerY);
    rect2.set(rect.left, rect.top + 63, rect.left + 68, rect.top + 128);
    rect3.set(rect.left - 26, rect.top + 32, rect.left, rect.top + 52);
    rect4.set(rect.left + 68, rect.top + 32, rect.left + 94, rect.top + 52);
    yellowRed.set(centerX - 100, centerY - 100, centerX + 70, centerY + 70);
    footleft.set(centerX - 50, centerY + 20, centerX, centerY + 35);
    footright.set(centerX, centerY + 20, centerX + 50, centerY + 35);
  }

  public void moveRight() {
    if (!ducked) {
      speedX = MOVESPEED;
    }
  }

  public void moveLeft() {
    if (!ducked) {
      speedX = -MOVESPEED;
    }
  }

  public void stopRight() {
    setMovingRight(false);
    stop();
  }

  public void stopLeft() {
    setMovingLeft(false);
    stop();
  }

  private void stop() {
    if (isMovingRight() == false && isMovingLeft() == false) {
      speedX = 0;
    }

    if (isMovingRight() == false && isMovingLeft() == true) {
      moveLeft();
    }

    if (isMovingRight() == true && isMovingLeft() == false) {
      moveRight();
    }
  }

  public void jump() {
    if (!jumped) {
      speedY = JUMPSPEED;
      jumped = false;
    }
  }

  public void shoot() {
    if (readyToFire) {
      Projectile p = new Projectile(centerX + 50, centerY - 25);
      mProjectiles.add(p);
    }
  }

  public int getCenterX() {
    return centerX;
  }

  public void setCenterX(int centerX) {
    this.centerX = centerX;
  }

  public int getCenterY() {
    return centerY;
  }

  public void setCenterY(int centerY) {
    this.centerY = centerY;
  }

  public boolean isJumped() {
    return jumped;
  }

  public void setJumped(boolean jumped) {
    this.jumped = jumped;
  }

  public int getSpeedX() {
    return speedX;
  }

  public void setSpeedX(int speedX) {
    this.speedX = speedX;
  }

  public int getSpeedY() {
    return speedY;
  }

  public void setSpeedY(int speedY) {
    this.speedY = speedY;
  }

  public boolean isDucked() {
    return ducked;
  }

  public void setDucked(boolean ducked) {
    this.ducked = ducked;
  }

  public boolean isMovingRight() {
    return movingRight;
  }

  public void setMovingRight(boolean movingRight) {
    this.movingRight = movingRight;
  }

  public boolean isMovingLeft() {
    return movingLeft;
  }

  public void setMovingLeft(boolean movingLeft) {
    this.movingLeft = movingLeft;
  }

  public boolean isReadyToFire() {
    return readyToFire;
  }

  public void setReadyToFire(boolean readyToFire) {
    this.readyToFire = readyToFire;
  }

  public ArrayList<Projectile> getProjectiles() {
    return mProjectiles;
  }

  public void setProjectiles(ArrayList<Projectile> projectiles) {
    mProjectiles = projectiles;
  }
}
